using System;
using System.Collections.Specialized;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;

namespace OR.Reuse.Persistence
{
    /// <summary>
    /// Summary description for DataUtility.
    /// </summary>
    public class DataUtility
    {
        /// <summary>
        /// Returns a Camel-Cased string.  For example, MyObject will become myObject.
        /// If the sourceValue does not start with a capital letter, the output will
        /// be identical to the input value.
        /// </summary>
        /// <param name="sourceValue">String to reformat.</param>
        /// <returns>A camel-cased string.</returns>
        public static string CamelCase(string sourceValue)
        {
            string rtnVal = null;

            if (!String.IsNullOrEmpty(sourceValue))
            {
                rtnVal = sourceValue[0].ToString().ToLower();
                rtnVal += sourceValue.Substring(1);
            }

            return rtnVal;
        }

        /// <summary>
        /// Determines whether or not the input string has content.  This does not determine whether
        /// or not the content is whitespace only.
        /// </summary>
        /// <param name="p_szSource">The string to check.</param>
        /// <returns>A boolena indicating whether or not the string has content.</returns>
        public static bool IsStringEmpty(string p_szSource)
        {
            return ((p_szSource == null) || (p_szSource == String.Empty));
        }

        /// <summary>
        /// Returns a boolean value indicating whether the specified value equates to "Empty".  For non-nullable scalar values
        /// (Int32, DateTime, etc.), the current value is compared against the type's "MinValue" property.
        /// </summary>
        /// <param name="pSource">Value to check for empty.</param>
        /// <returns>True if the value is Empty, False if it contains a value.</returns>
        public static bool IsEmpty(object pSource)
        {
            bool vReturn = false;

            if (pSource == null)
            {
                vReturn = true;
            }
            else if (pSource is String)
            {
                vReturn = ((String)pSource == String.Empty);
            }
            else if (pSource is Int32)
            {
                vReturn = ((Int32)pSource == Int32.MinValue);
            }
            else if (pSource is Int16)
            {
                vReturn = ((Int16)pSource == Int16.MinValue);
            }
            else if (pSource is Int64)
            {
                vReturn = ((Int64)pSource == Int64.MinValue);
            }
            else if (pSource is DateTime)
            {
                vReturn = ((DateTime)pSource == DateTime.MinValue);
            }
            else if (pSource is float)
            {
                vReturn = ((float)pSource == float.MinValue);
            }
            else if (pSource is Decimal)
            {
                vReturn = ((Decimal)pSource == Decimal.MinValue);
            }
            else if (pSource is Double)
            {
                vReturn = ((Double)pSource == Double.MinValue);
            }

            return (vReturn);
        }

        public static IComparable GetComparable(DataRow p_drSource, String p_szColumnName)
        {
            return (DataUtility.GetComparable(p_drSource, p_szColumnName, null));
        }

        public static IComparable GetComparable(DataRow p_drSource, String p_szColumnName, IComparable p_oDefault)
        {
            IComparable v_oReturn = p_oDefault;
            DataColumn v_dcColumn = p_drSource.Table.Columns[p_szColumnName];
            object v_oValue = p_drSource[p_szColumnName];

            if (v_dcColumn == null)
            {
                throw new ApplicationException(String.Format("DataUtility.GetComparable failed: The column [{0}] cannot be found in the DataTable [{1}].", p_szColumnName, p_drSource.Table.TableName));
            }
            else
            {
                if (v_dcColumn.DataType == typeof(Decimal))
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = (Decimal)v_oValue;
                    }
                }
                else if (v_dcColumn.DataType == typeof(DateTime))
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = (DateTime)v_oValue;
                    }
                }
                else if (v_dcColumn.DataType == typeof(Int32))
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = (Int32)v_oValue;
                    }
                }
                else if (v_dcColumn.DataType == typeof(Int64))
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = Convert.ToInt32(v_oValue);
                    }
                }
                else if (v_dcColumn.DataType == typeof(bool))
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = (bool)v_oValue;
                    }
                }
                else
                {
                    if (v_oValue != DBNull.Value)
                    {
                        v_oReturn = (String)v_oValue;
                    }
                }
            }

            if (v_oReturn == null)
            {
                return (p_oDefault);
            }
            else
            {
                return (v_oReturn);
            }
        }

        public static object GetString(IComparable pValue)
        {
            try
            {
                String mValue = Convert.ToString(pValue);
                return mValue;
            }
            catch
            {
                return (null);
            }
        }


        public static string GetString(String p_szFilename)
        {
            StreamReader v_oReader = new StreamReader(new FileStream(p_szFilename, FileMode.Open));

            StringBuilder v_oBuilder = new System.Text.StringBuilder(); // you can specify a Capacity here ,  in the  (  )  brackets.

            while (v_oReader.Peek() != -1)
            {
                v_oBuilder.Append(v_oReader.ReadLine() + "\n");
            }

            v_oReader.Close();

            return (v_oBuilder.ToString());
        }

        public static string GetString(XmlElement p_elElement, string p_szAttribute)
        {
            return DataUtility.GetString(p_elElement, p_szAttribute, null);
        }

        public static string GetString(XmlElement p_elElement, string p_szAttribute, string p_szDefault)
        {
            string v_szResult = String.Empty;
            XmlAttribute v_oAttribute = (XmlAttribute)p_elElement.SelectSingleNode("@" + p_szAttribute);

            if (v_oAttribute == null)
            {
                v_szResult = p_szDefault;
            }
            else
            {
                v_szResult = v_oAttribute.Value;
            }

            return (v_szResult);
        }

        public static string GetString(XPathNavigator p_elElement, string p_szAttribute)
        {
            return DataUtility.GetString(p_elElement, p_szAttribute, null);
        }

        public static string GetString(XPathNavigator p_elElement, string p_szAttribute, string p_szDefault)
        {
            string v_szResult = p_elElement.GetAttribute(p_szAttribute, String.Empty);

            if (v_szResult == null)
            {
                v_szResult = p_szDefault;
            }

            return (v_szResult);
        }

        public static string GetString(DataRow p_drRow, string p_szColumn)
        {
            return DataUtility.GetString(p_drRow, p_szColumn, String.Empty);
        }

        public static string GetString(DataRow p_drRow, string p_szColumn, string p_szDefault)
        {
            try
            {
                string v_szResult = "";
                object v_oTupol = p_drRow[p_szColumn];

                if (v_oTupol != DBNull.Value)
                {
                    v_szResult = v_oTupol.ToString();
                }
                else
                {
                    v_szResult = p_szDefault;
                }

                return (v_szResult);
            }
            catch (ArgumentNullException)
            {
                return (p_szDefault);
            }
            catch (Exception)
            {
                // throw ex;

                return (null);
            }
        }

        public static string GetString(NameValueCollection p_nvcSource, string p_szKey)
        {
            return (p_nvcSource[p_szKey]);
        }

        public static Color GetColor(DataRow p_drRow, string p_szColumn)
        {
            return (DataConverter.ToColor(DataUtility.GetString(p_drRow, p_szColumn)));
        }

        public static Color GetColor(XmlElement p_elElement, string p_szAttribute)
        {
            return (DataConverter.ToColor(DataUtility.GetString(p_elElement, p_szAttribute)));
        }

        public static Guid GetGuid(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);
                Guid v_uResult = Guid.Empty;

                if (v_szResult != null)
                {
                    v_uResult = new Guid(v_szResult);
                }

                return (v_uResult);
            }
            catch
            {
                return (Guid.Empty);
            }
        }

        public static Guid GetGuid(XmlElement p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);
                Guid v_uResult = Guid.Empty;

                if (v_szResult != null)
                {
                    v_uResult = new Guid(v_szResult);
                }

                return (v_uResult);
            }
            catch
            {
                return (Guid.Empty);
            }
        }

        public static Guid GetGuid(XPathNavigator p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);
                Guid v_uResult = Guid.Empty;

                if (v_szResult != null)
                {
                    v_uResult = new Guid(v_szResult);
                }

                return (v_uResult);
            }
            catch
            {
                return (Guid.Empty);
            }
        }

        public static Guid GetGuid(NameValueCollection p_nvcSource, string p_szKey)
        {
            try
            {
                return (new Guid(DataUtility.GetString(p_nvcSource, p_szKey)));
            }
            catch
            {
                return (Guid.Empty);
            }
        }

        public static object GetBool(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult == null || v_szResult == "")
                {
                    return (null);
                }

                return ((v_szResult == "1") || (v_szResult.ToLower() == "true"));
            }
            catch
            {
                return (null);
            }
        }

        public static bool GetBool(DataRow p_drRow, string p_szColumn, bool p_bDefault)
        {
            object vReturn = GetBool(p_drRow, p_szColumn);

            if (vReturn == null)
            {
                vReturn = p_bDefault;
            }

            return ((bool)vReturn);
        }

        public static object GetBool(XmlElement p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null || v_szResult == "")
                {
                    return (null);
                }

                return ((v_szResult == "1") || (v_szResult.ToLower() == "true"));
            }
            catch
            {
                return (null);
            }
        }

        public static bool GetBool(XmlElement p_elRow, string p_szColumn, bool p_bDefault)
        {
            object vReturn = GetBool(p_elRow, p_szColumn);

            if (vReturn == null)
            {
                vReturn = p_bDefault;
            }

            return ((bool)vReturn);
        }

        public static bool GetBool(XPathNavigator p_elRow, string p_szColumn)
        {
            return (GetBool(p_elRow, p_szColumn, false));
        }

        public static bool GetBool(XPathNavigator p_elRow, string p_szColumn, bool p_bDefault)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null || v_szResult == "")
                {
                    return (p_bDefault);
                }

                return ((v_szResult == "1") || (v_szResult.ToLower() == "true"));
            }
            catch
            {
                return (p_bDefault);
            }
        }

        public static bool GetBool(NameValueCollection p_nvcSource, string p_szKey)
        {
            return (DataUtility.GetBool(p_nvcSource, p_szKey, false));
        }

        public static bool GetBool(NameValueCollection p_nvcSource, string p_szKey, bool p_bDefault)
        {
            string v_szResult = DataUtility.GetString(p_nvcSource, p_szKey);

            if (v_szResult == null)
            {
                return (p_bDefault);
            }
            else
            {
                try
                {
                    return ((v_szResult == "1") || (v_szResult.ToLower() == "true"));
                }
                catch
                {
                    return (p_bDefault);
                }
            }
        }

        public static object GetDecimal(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDecimal(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Decimal GetDecimal(DataRow p_drRow, string p_szColumn, Decimal p_dDefault)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDecimal(v_szResult));
                }
                else
                {
                    return (p_dDefault);
                }
            }
            catch
            {
                return (p_dDefault);
            }
        }


        public static object GetDouble(IComparable p_Value)
        {
            try
            {
                if (p_Value != null)
                {
                    return (Convert.ToDouble(p_Value));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static object GetDouble(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDouble(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Double GetDouble(DataRow p_drRow, string p_szColumn, Double p_dDefault)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDouble(v_szResult));
                }
                else
                {
                    return (p_dDefault);
                }
            }
            catch
            {
                return (p_dDefault);
            }
        }

        public static object GetDouble(XmlElement p_elRow, string p_szAttribute)
        {
            try
            {
                string v_szResult = DataUtility.GetString(p_elRow, p_szAttribute);

                if (v_szResult != null)
                {
                    return (Convert.ToDouble(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Double GetDouble(XmlElement p_elRow, string p_szAttribute, Double p_dDefault)
        {
            try
            {
                string v_szResult = DataUtility.GetString(p_elRow, p_szAttribute);

                if (v_szResult != null)
                {
                    return (Convert.ToDouble(v_szResult));
                }
                else
                {
                    return (p_dDefault);
                }
            }
            catch
            {
                return (p_dDefault);
            }
        }

        public static object GetDateTime(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static object GetDateTime(IComparable p_DateTime)
        {
            try
            {
                String mDateTime = Convert.ToString(p_DateTime);
                if (p_DateTime != null && !p_DateTime.Equals(String.Empty))
                {
                    return (Convert.ToDateTime(p_DateTime));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }


        public static DateTime GetDateTime(DataRow p_drRow, string p_szColumn, DateTime p_dtDefault)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (p_dtDefault);
                }
            }
            catch
            {
                return (p_dtDefault);
            }
        }

        public static object GetDateTime(XmlElement p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static DateTime GetDateTime(XmlElement p_elRow, string p_szColumn, DateTime p_dtDefault)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (p_dtDefault);
                }
            }
            catch
            {
                return (p_dtDefault);
            }
        }

        public static object GetDateTime(XPathNavigator p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (null);
                }
            }
            catch
            {
                return (null);
            }
        }

        public static DateTime GetDateTime(XPathNavigator p_elRow, string p_szColumn, DateTime p_dtDefault)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult != null)
                {
                    return (Convert.ToDateTime(v_szResult));
                }
                else
                {
                    return (p_dtDefault);
                }
            }
            catch
            {
                return (p_dtDefault);
            }
        }

        public static object GetInt32(IComparable p_Value)
        {
            try
            {

                if (p_Value == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt32(p_Value));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static object GetInt32(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt32(v_szResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int32 GetInt32(DataRow p_drRow, string p_szColumn, Int32 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt32(p_drRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int32)v_oValue;
        }

        public static object GetInt32(NameValueCollection pSource, string pColumn)
        {
            try
            {
                string vResult = GetString(pSource, pColumn);

                if (vResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt32(vResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int32 GetInt32(NameValueCollection pSource, string pName, Int32 pDefault)
        {
            object vValue = DataUtility.GetInt32(pSource, pName);

            if (vValue == null)
            {
                vValue = pDefault;
            }

            return (Int32)vValue;
        }

        public static object GetInt32(XmlElement p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt32(v_szResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int32 GetInt32(XmlElement p_elRow, string p_szColumn, Int32 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt32(p_elRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int32)v_oValue;
        }

        public static object GetInt32(XPathNavigator p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt32(v_szResult));
                }
            }
            catch
            {

                return (null);
            }
        }

        public static Int32 GetInt32(XPathNavigator p_elRow, string p_szColumn, Int32 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt32(p_elRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int32)v_oValue;
        }

        public static object GetInt64(IComparable p_Value)
        {
            try
            {

                if (p_Value == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt64(p_Value));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static object GetInt64(DataRow p_drRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_drRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt64(v_szResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int64 GetInt64(DataRow p_drRow, string p_szColumn, Int64 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt64(p_drRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int64)v_oValue;
        }

        public static object GetInt64(NameValueCollection pSource, string pColumn)
        {
            try
            {
                string vResult = GetString(pSource, pColumn);

                if (vResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt64(vResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int64 GetInt64(NameValueCollection pSource, string pName, Int64 pDefault)
        {
            object vValue = DataUtility.GetInt64(pSource, pName);

            if (vValue == null)
            {
                vValue = pDefault;
            }

            return (Int64)vValue;
        }

        public static object GetInt64(XmlElement p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt64(v_szResult));
                }
            }
            catch
            {
                return (null);
            }
        }

        public static Int64 GetInt64(XmlElement p_elRow, string p_szColumn, Int64 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt64(p_elRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int64)v_oValue;
        }

        public static object GetInt64(XPathNavigator p_elRow, string p_szColumn)
        {
            try
            {
                string v_szResult = GetString(p_elRow, p_szColumn);

                if (v_szResult == null)
                {
                    return (null);
                }
                else
                {
                    return (Convert.ToInt64(v_szResult));
                }
            }
            catch
            {

                return (null);
            }
        }

        public static Int64 GetInt64(XPathNavigator p_elRow, string p_szColumn, Int64 p_iDefault)
        {
            object v_oValue = DataUtility.GetInt64(p_elRow, p_szColumn);

            if (v_oValue == null)
            {
                v_oValue = p_iDefault;
            }

            return (Int64)v_oValue;
        }

        static DataUtility()
        {
        }
    }
}